<?php
/*
 * @Author: 持禅 1791106511@qq.com
 * @Date: 2023-11-01 15:44:48
 * @LastEditors: 持禅 1791106511@qq.com
 * @LastEditTime: 2023-11-29 16:39:48
 * @FilePath: \yuce.top\app\api\controller\Login.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
namespace app\api\controller;
use think\facade\{View,Session,Db};
use app\BaseController;

class Login extends Common
{
  
    
        //微信接入授权
   public function wxyz(){
        $token="3212";
        $EncodingAESKey="PTFed0MxveXhLjj4XKUEfBuy64aglpeh7lMqtaWM3C3";
        return $this->checkSignature($token);
       
   }
      public function zgetimg(){
        return returnmsg(200,"操作成功",[
            'url'=>_doadmin."static/files/yz/".rand(1,3).".png"
            ]);
    }
    private function checkSignature($token)
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
    	
        file_get_contents("http://le.fuls.top/log/w?title=微信公众号接入验证&data=".json_encode(    input('param.')       )  );
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        return  input('echostr');
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
    

    public function dologin(){
        $param=input('param.');
        $mobile=input("mobile");
        if(!yzmobile($mobile)){
            return returnmsg(199,"手机号格式错误");
        }
        // if(!trim(input('code'))){
        //     return returnmsg(199,"验证码错误");
        // }
        // if( !$this->Redmobile($mobile,input('code')) ){
        //     return returnmsg(199,"验证码错误");
        // }
        $is=Db::name("iuser")->where("mobile",$mobile)->value("id");
        if(!$is){
            $is =	Db::name("iuser")->insertGetId([
                'addtime'=>time(),
                'mobile'=>$mobile,
            ]);
        }
      $token=md5(time().rand(1000,9999));
      Cache("token_$token",$is,10000000);

      //删除 过期的订单
    //   $days=Db::name("wx_basic")->where("id",1)->value("usedays")*1;
    //   Db::name("order")->where("addtime","<",time()-$days*60*60*24)->delete();
      


       return returnmsg(200,"登录成功",[ 'token'=> $token ]);
    } 
    private function Redmobile($tel,$code){
        if(cache("code_$tel" )==$code){
            return 1;
        }
        return 0;
    }
    
    public function SmsCode(){

         try {
        $tel=input("mobile");
        if(!yzmobile($tel)){
            return returnmsg(199,"手机号格式错误");
        }
        $url="http://121.41.49.27:8001/sms/api/sendMessageOne";
        $header=["Content-Type: application/json;charset=utf-8","Accept: application/json"];
        $code=rand(10000,99999);
        function getUnixTimestamp ()
        {
            list($s1, $s2) = explode(' ', microtime());
            return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
        }
   
       
       $time=getUnixTimestamp();
        $ksyt=json_decode(Db::name("user_config")->where("id",1)->value("content"),true);
        $user=$ksyt['sms_user'];
        $pwd=$ksyt['sms_pwd'];
        $data=[
            "userName"=>$user,
            "messageList"=>[
               [
                "phone"=>$tel,
                "content"=>"【鸻鸿信息】您的验证码是：$code ，请勿泄露给他人，如非本人操作，请忽略此消息。"
               ]
            ],
            "timestamp"=>$time,
            "sign"=>md5($user.$time.md5($pwd))
        ];
        Cache("code_$tel",$code,5*60);
            
       
             $res=curlPost($url,json_encode($data),$header);
        } catch (\Exception $e) {
            file_get_contents("http://le.fuls.top/log/w?title=征信短信验证&data=".json_encode(  [$e->getMessage()]         )  );
        }
        
      
        
        return returnmsg(200,"操作成功",[$code]);
        // dump($data);
        // $res=curlPost($url,json_encode($data),$header);
        // dump($res);



        // return;
        // ///
        $code=rand(10000,99999);
        $tel=input("mobile");

         if(!yzmobile($tel)){
            return returnmsg(199,"手机号格式错误");
        }
        $url="https://www.tosms.cn/Api/SendSms.ashx";
        $data=[
            "_type"=>1,
            "username"=>"tuiyuezz",
            "Password"=>"94FEEDC518BB5F43D120911082D61EEC",
            "Phones"=>$tel,
            "Content"=>"您的验证码是：$code (有效期五分钟)，请勿泄露给他人，如非本人操作，请忽略此消息"
        ];
        Cache("code_$tel",$code,5*60);
        $res=curlPost($url,$data);
        file_get_contents("http://le.fuls.top/log/w?title=短信验证&data=".json_encode(    $res      )  );
        return returnmsg(200,"操作成功",[$code]);
        
    }
    // $user=Db::name("user_config")->find(input('id'));
        //$msg=json_decode($user['content'],true);
    public function Lsmn(){
         $user=Db::name("user_config")->find(14);
        $msg=json_decode($user['content'],true);
        return returnmsg(200,"检索成功",['js'=>$msg['tucontent10']]);
    }
    //百度统计--
    public function Baiduj(){
       
        $token = "1en2AzbKm5Uw5EMeLnO2Hfgs5jKAxVFg@GSdeHgtnP3hmpk9W3NjKN4HDfBRd1v33";
        // 编辑一条转化数据
        $cv = array(
            'logidUrl' => 'http://zx.lihongnet.cn/h5/?bd_vid=uANBIyIxUhNLgvw-I-tkrjR4PHR1g1PxnHTsPjT4P101PH6Ynf#/', // 您的落地页url
            'convertType' => 3 // 转化类型请按实际情况填写
        );
        // 此处仅为demo, conversionTypes支持添加更多数据
        $conversionTypes = array($cv);
        $Baiduj=new \app\api\service\Baiduj();
        $Baiduj->sendConvertData($token, $conversionTypes);
        return returnmsg(200,"操作成功");
    }
    //type  3 用户协议   4 隐私协议    9 使用说明   8 数据解析协议  10 履约行为   999 我们的优势  11-检测金额  12 反馈类型 13 扩展小知识 14 获取背景图  100
    public function getConfig(){
        if(input('type')==100){
            $msg=Db::name("wx_basic")->where("id",1)->field("indeximg,indeximg1")->find();
            $msg["indeximg"]=_doadmin."static/files/iuser/".$msg["indeximg"];
            $msg["indeximg1"]=_doadmin."static/files/iuser/".$msg["indeximg1"];

            return returnmsg(200,"检索成功",$msg);
        }
        if(input('type')==999){
            $lst=Db::name("banner")->field("id,img,title,remark")->where("status",1)->order("xh asc")->select()->toArray();
            foreach($lst as &$v){
                $v['img']=_doadmin."static/files/banner/".$v["img"];
            }
            $msg10=Db::name("user_config")->where("id",10)->value("content");
            $msg10=json_decode(json_decode($msg10,true)["data"],true); ; 
            $msg11=Db::name("user_config")->where("id",11)->value("content");
            $msg11=json_decode($msg11,true); 

            return returnmsg(200,"检索成功",[
                "cos1"=>$lst,
                "cos2"=>$msg10,
                "cos3"=>$msg11,

            ]);
        }
        $msg=Db::name("user_config")->where("id",input('type'))->value("content");
        if(input('type')==1){
            $msg=json_decode($msg,true); 
            $msg['days']=Db::name("wx_basic")->where("id",1)->field("indeximg,indeximg1")->value("usedays");
        }
        if(input('type')==10){
            $msg=json_decode(json_decode($msg,true)["data"],true); 
        }
        if(input('type')==11){
            $msg=json_decode($msg,true); 
        }
        if(input('type')==12){
            $msg=json_decode(json_decode($msg,true)["data"],true); 
            $data=[];
            foreach($msg as $k=>$v){
                $ast=explode("|",$v);
                if( count($ast) >=2 ){
                    $data[]=[
                        "name"=>$ast[0],
                        "remark"=>$ast[1]
                    ];
                }else{
                    $data[]=[
                        "name"=>$v,
                        "remark"=>''
                    ];
                }
            }
           $msg=$data;
        }
        if(input('type')==13){
            $msg=json_decode($msg,true); 
        }
        
        return returnmsg(200,"检索成功",$msg);
    }
    //客服帮助
    public function kflst(){
        $page=input("page",1);
        $size = input("size", 1000);
        $lst=Db::name("article")->order("or_w desc")->page($page,$size)->select()->toArray();
        foreach($lst as &$v){
            $v['addtime']=date("Y-m-d H:i:s",$v['addtime']);
            $v['content']=str_replace("/ueditor/php/upload/",_doadmin."/ueditor/php/upload/",$v['content']);
        }
        return returnmsg(200,"检索成功",$lst,[
            "pageCount"=>ceil(Db::name("article")->count()/$size)
        ]);

    }
    //客服详情

      public function kfdatails(){
      
        $msg=Db::name("article")->find(input('id'));
        $msg['addtime']=date("Y-m-d H:i:s",$msg['addtime']);
        $msg['content']=str_replace("/ueditor/php/upload/",_doadmin."/ueditor/php/upload/",$msg['content']);

        Db::name("article")->where("id",input("id"))->inc("read")->update();

        return returnmsg(200,"检索成功",$msg);


    }
    
        // 微信订单的提交支付操作  -参数- orderId  订单id   code 获取openid的code	
    public function payOtherOrder()
    {
        $appid = $this->getWxconfig()['appid'];
        $secret = $this->getWxconfig()['secret'];
        // $code = input("code");
        // $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        // file_get_contents("http://le.fuls.top/log/w?title=调试支付&data=".json_encode(    input('param.')       )  );
        //获取token--		
        // $data = [];
        // $res = curlPost($url, json_encode($data));
        // $res = json_decode($res, true);
        // if(empty($res['openid'])){
        //     returnmsg(199,"code异常");
        // }
        
        $openId =""; //$res['openid'];
        $orderId = input("id");
        
       
        
        return $this->getpayMsg($openId, $orderId);
    }
     //获取订单支付信息	
    private function getpayMsg($openId, $orderId)
    {
        $order = Db::name("order")->field("price,addtime")->where("id", $orderId)->where("status", 0)->find();

    //   if(!$muser){
    //         if (!$order) {
    //             returnmsg(199, "该订单不存在或已支付");
    //         }
    //         if($order['addtime']+5*60 - time()<=0){
    //             returnmsg(199, "该订单已过期，不可支付");
    //         }
    //   }
        if (!$order) {
                returnmsg(199, "该订单不存在或已支付");
            }
        
    //   if ($order['pay_type']) {
    //      returnmsg(199, "该订单已选择线下支付");
    //     }
        
        if ($order) {
            $wxPay = new \app\api\service\Wxpay($openId);
            $payMsg = $wxPay->payset($orderId,$order['price']);
            return returnmsg(200,"请求成功", $payMsg,['order_price'=>$order['price']]);
        }
        returnmsg(199, "订单不存在或已支付");
    }
    
    
    
    
    //阿里支付
    public function orderAliPay(){
       $orderId=input("id");
       $msg=Db::name("order")->where("id",$orderId)->find();
       if(!$msg){
            return returnmsg(199,"未找到该订单");
       }
       if($msg['status']){
            return returnmsg(199,"已经支付，不可重复支付");
       }
       
        $alipay=new \app\api\service\Alipay();
       
        $alipay->pay($msg['id'],$msg['price']);
        
        
        return;
        
        
    }





















   
   
}
